# -*- coding: utf-8 -*-
# Created Time : 2022/1/5 15:51
# Author:Zhou
import os

import h5py
import numpy as np
from ANC_tools import sin_wave, awgn, NPP_1, NPP_2, NPP_3, NPP_4, NPP_5, logisticChaotic
import scipy.io as scio

# some settings
dataFile = 'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/leopard_norm.mat'
data = scio.loadmat(dataFile)
noiseSource = data['data']
NPP = NPP_4
rms = 1.0
samplingRate = 16000
noiseLength = 6  # 时间 单位s  可以和训练样本长度不同

n_tt_ex = 1
filepath = 'datasets/hybrid_NPP4/tt/'
filename = 'tt.ex'
# filename = 'tt_snr-0.ex'
# filename = 'tt_snr5.ex'
writer = h5py.File(os.path.join(filepath, filename), 'w')
for idx in range(n_tt_ex):
    # generate a noisy mixture
    print('generating: sample_{}'.format(idx))

    # mix = logisticChaotic(0.9, 4, 16000)
    # mix = awgn(ref, 40)

    beginning = np.random.randint(0, len(noiseSource) - noiseLength * samplingRate)
    end = beginning + noiseLength * samplingRate
    print(beginning)
    mix = noiseSource[beginning: end, :]
    sph = NPP(mix)
    # normalize
    c = rms * np.sqrt(mix.size / np.sum(mix ** 2))
    mix *= c
    sph *= c

    writer_grp = writer.create_group(str(idx))
    writer_grp.create_dataset('mix', data=mix.astype(np.float32), shape=mix.shape, chunks=True)
    writer_grp.create_dataset('sph', data=sph.astype(np.float32), shape=sph.shape, chunks=True)
writer.close()
